package com.dmlll.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmlll.common.Constant;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.HospitalPurchaseInput;
import com.dmlll.domain.Purchase;
import com.dmlll.domain.PurchaseDrupDetail;
import com.dmlll.service.HospitalPurchaseInputService;
import com.dmlll.service.PurchaseDrupDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医院交易采购单入库表 前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
@RequestMapping("hpi")
public class HospitalPurchaseInputController {
    @Autowired
    private HospitalPurchaseInputService hospitalPurchaseInputService;
    @Autowired
    private PurchaseDrupDetailService purchaseDrupDetailService;
    /**
     * 批量添加，修改采购状态
     * @param purchaseStr
     * @return
     */
    @RequestMapping("add")
    public ResultObj add(String purchaseStr){
        List<PurchaseDrupDetail> purchaseDrupDetails = JSONArray.parseArray(purchaseStr, PurchaseDrupDetail.class);
        //获取采购详情列表，修改采购已入库状态
        List<HospitalPurchaseInput> inputs = new ArrayList<>();
        for (PurchaseDrupDetail purchaseDrupDetail:purchaseDrupDetails){
            purchaseDrupDetail.getInput().setInputTime(new Date());
            inputs.add(purchaseDrupDetail.getInput());
            purchaseDrupDetail.setPurchaseState(Constant.PURCHASE_STATE_THREE);
        }
        try {
            purchaseDrupDetailService.updateBatchById(purchaseDrupDetails);
            hospitalPurchaseInputService.saveBatch(inputs);
        } catch (Exception e) {
            return ResultObj.INPUT_ERROR;
        }
        return new ResultObj(Constant.OK,Constant.INPUT_SUCCESS,purchaseDrupDetails.size());
    }
}

